/**
 * Created by ZZT on 2017/8/20.
 */

var Carousel = {
    cObj:$id("carousel"),
    h : $id("header"),
    u : $id("carousel").getElementsByTagName("ul"),
    d : $id("navBar").getElementsByTagName("span"),
    cTimer: null,
    loop:0,
    /*主要驱动函数：切换下一张图片*/
    invoke:function () {
        var u = this.u[0],
            ul = parseInt(u.style.left),
            len = this.d.length;
        u.style.left = ( ul <= (-1920*(len-1)) ) ? ( 0+"px"):(( ul -1920)+"px");
        this.dotSet();
    },
    /*自动轮播函数*/
    autoPlay:function () {
        var _this = this;
        this.cTimer = setInterval(function () {
            _this.invoke();
        },5000)
    },
    /*点击导航点，跳转对应图片函数*/
    dotClick:function () {
        var d = this.d,
            u = this.u[0],
            _this = this;
        $addEvent(d,"click",function () {
            var i = parseInt(this.getAttribute("data-index"));
            u.style.left = -1920*i + "px";
            _this.dotSet(i);
        })
    },
    /*导航点设置函数：未激活状态与激活*/
    dotSet:function (t) {
        var d = this.d,
            len = this.d.length,
            s = this.loop;
        $removeClass(this.d[s],"navDotsOn");
        if(t === undefined){
            this.loop++;
            if(this.loop > (len-1)) this.loop = 0;
        }else {
            this.loop = t;
        }
        t = this.loop;
        $addClass(d[t],"navDotsOn");
        if(t === 0 || t === 2){
            $addClass(this.h,"theme-grad");
        }else {
            $removeClass(this.h,"theme-grad");
        }
    },
    init:function (t) {
        this.autoPlay();
        this.dotClick();
    }
};
Carousel.init();

